/*
 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/* QSFP support common definitions, for ib_qib driver */
#include "stdio.h"
#include "stdint.h"

#define QSFP_DEV 0x50

//状态指示位
typedef union
{
    uint16_t status;
    struct
    {
        uint16_t  data_not_ready:1;//指示数据是否已准备好
        uint16_t  initL:1;//intl中断输出引脚读数字状态
        uint16_t  flag_mem:1;//内存直接分页标志0：更多分页，1：只有page0
        uint16_t  reserve:13;
            
    }byte2;
}qsfp_4x_status;

//通道状态中断标志
typedef union
{
    uint8_t losFlag;
    struct
    {
        uint8_t  rx1_los:1;//锁存的RX los标志，通道1
        uint8_t  rx2_los:1;//锁存的RX los标志，通道2
        uint8_t  rx3_los:1;//锁存的RX los标志，通道3
        uint8_t  rx4_los:1;//锁存的RX los标志，通道4
        uint8_t  tx1_los:1;//锁存的TX los标志，通道1
        uint8_t  tx2_los:1;//锁存的TX los标志，通道2
        uint8_t  tx3_los:1;//锁存的TX los标志，通道3
        uint8_t  tx4_los:1;//锁存的TX los标志，通道4
            
    }byte3;
}qsfp_4x_intc_flag0;

//TX fault指示器
typedef union
{
    uint8_t txFault;
    struct
    {
        uint8_t  tx1_fault:1;//锁存TX故障指示灯，通道1
        uint8_t  tx2_fault:1;//锁存TX故障指示灯，通道2
        uint8_t  tx3_fault:1;//锁存TX故障指示灯，通道3
        uint8_t  tx4_fault:1;////锁存TX故障指示灯，通道4
        uint8_t  reserve:4;//预留
            
    }byte4;
}qsfp_4x_fault;

//温度模块监控器
typedef union
{
    uint8_t tempAlarm;
    struct
    {
        uint8_t  initFlag:1;//0:正在初始化 1：可读
        uint8_t  reserve:3;//预留
        uint8_t  lTempWarn:1;//锁存低温警告
        uint8_t  hTempWarn:1;//锁存高温警告
        uint8_t  lTempAlarm:1;//锁存低温报警
        uint8_t  hTempAlarm:1;//锁存高温报警
            
    }byte6;
    
}qsfp_4x_monitorTamp;

//电压模块监控器
typedef union
{
    uint8_t voltAlarm;
    struct
    {
        uint8_t  reserve:4;//预留
        uint8_t  lVoltWarn:1;//锁存低电源电压警告
        uint8_t  hVoltWarn:1;//锁存高电源电压警告
        uint8_t  lVoltAlarm:1;//锁存低电源电压报警
        uint8_t  hVoltAlarm:1;//锁存高电源电压报警
            
    }byte7;
}qsfp_4x_monitorVolt;

//功率模块监控器1
typedef union
{
    uint8_t rxPwdAlarm;
    struct 
    {
        uint8_t  rx2_lPwdWarn:1;//RX2锁存功率过低警告
        uint8_t  rx2_hPwdWarn:1;//RX2锁存功率过高警告
        uint8_t  rx2_lPwdAlarm:1;//RX2锁存功率过低报警
        uint8_t  rx2_hPwdAlarm:1;//RX2锁存功率过高报警
        uint8_t  rx1_lPwdWarn:1;//RX1锁存功率过低警告
        uint8_t  rx1_hPwdWarn:1;//RX1锁存功率过高警告
        uint8_t  rx1_lPwdAlarm:1;//RX1锁存功率过低报警
        uint8_t  rx1_hPwdAlarm:1;//RX1锁存功率过高报警
    }byte9;
}qsfp_4x_monitorRx1;

//功率模块监控器2
typedef union
{
    uint8_t rxPwdAlarm;
    struct 
    {
        uint8_t  rx4_lPwdWarn:1;//RX4锁存功率过低警告
        uint8_t  rx4_hPwdWarn:1;//RX4锁存功率过高警告
        uint8_t  rx4_lPwdAlarm:1;//RX4锁存功率过低报警
        uint8_t  rx4_hPwdAlarm:1;//RX4锁存功率过高报警
        uint8_t  rx3_lPwdWarn:1;//RX3锁存功率过低警告
        uint8_t  rx3_hPwdWarn:1;//RX3锁存功率过高警告
        uint8_t  rx3_lPwdAlarm:1;//RX3锁存功率过低报警
        uint8_t  rx3_hPwdAlarm:1;//RX3锁存功率过高报警
    }byte10;
}qsfp_4x_monitorRx2;

//发射通道监控
typedef union
{
    uint8_t txBias;
    struct
    {
        uint8_t tx2_lTxBiasWarn:1;//内部监测读TX2偏置电流过低警告
        uint8_t tx2_hTxBiasWarn:1;//内部监测读TX2偏置电流过高警告
        uint8_t tx2_lTxBiasAlarm:1;//内部监测读TX2偏置电流过低报警
        uint8_t tx2_hTxBiasAlarm:1;//内部监测读TX2偏置电流过高报警
        uint8_t tx1_lTxBiasWarn:1;//内部监测读TX1偏置电流过低警告
        uint8_t tx1_hTxBiasWarn:1;//内部监测读TX1偏置电流过高警告
        uint8_t tx1_lTxBiasAlarm:1;//内部监测读TX1偏置电流过低报警
        uint8_t tx1_hTxBiasAlarm:1;//内部监测读TX1偏置电流过高报警
    }byte11;
}txBias1_4x;

typedef union
{
    uint8_t txBias;
    struct
    {
        uint8_t tx4_lTxBiasWarn:1;//内部监测读TX4偏置电流过低警告
        uint8_t tx4_hTxBiasWarn:1;//内部监测读TX4偏置电流过高警告
        uint8_t tx4_lTxBiasAlarm:1;//内部监测读TX4偏置电流过低报警
        uint8_t tx4_hTxBiasAlarm:1;//内部监测读TX4偏置电流过高警告
        uint8_t tx3_lTxBiasWarn:1;//内部监测读TX3偏置电流过低警告
        uint8_t tx3_hTxBiasWarn:1;//内部监测读TX3偏置电流过高警告
        uint8_t tx3_lTxBiasAlarm:1;//内部监测读TX3偏置电流过低报警
        uint8_t tx3_hTxBiasAlarm:1;//内部监测读TX3偏置电流过高警告
    }byte12;
}txBias2_4x;

typedef union
{
    uint8_t txPwdAlarm;
    struct 
    {
        uint8_t  tx2_lPwdWarn:1;//tx2锁存功率过低警告
        uint8_t  tx2_hPwdWarn:1;//tx2锁存功率过高警告
        uint8_t  tx2_lPwdAlarm:1;//tx2锁存功率过低报警
        uint8_t  tx2_hPwdAlarm:1;//tx2锁存功率过高报警
        uint8_t  tx1_lPwdWarn:1;//tx1锁存功率过低警告
        uint8_t  tx1_hPwdWarn:1;//tx1锁存功率过高警告
        uint8_t  tx1_lPwdAlarm:1;//tx1锁存功率过低报警
        uint8_t  tx1_hPwdAlarm:1;//tx1锁存功率过高报警
    }byte13;
}qsfp_4x_monitorTx1;

typedef union
{
    uint8_t txPwdAlarm;
    struct 
    {
        uint8_t  tx4_lPwdWarn:1;//tx4锁存功率过低警告
        uint8_t  tx4_hPwdWarn:1;//tx4锁存功率过高警告
        uint8_t  tx4_lPwdAlarm:1;//tx4锁存功率过低报警
        uint8_t  tx4_hPwdAlarm:1;//tx4锁存功率过高报警
        uint8_t  tx3_lPwdWarn:1;//tx3锁存功率过低警告
        uint8_t  tx3_hPwdWarn:1;//tx3锁存功率过高警告
        uint8_t  tx3_lPwdAlarm:1;//tx3锁存功率过低报警
        uint8_t  tx3_hPwdAlarm:1;//tx3锁存功率过高报警
    }byte14;
}qsfp_4x_monitorTx2;

//发射通道使能控制字
typedef union
{
    uint8_t txDisable;
    struct 
    {
        uint8_t  tx1_disable:1;//允许软件禁用发射机的读写位 1禁用 0启用
        uint8_t  tx2_disable:1;//允许软件禁用发射机的读写位
        uint8_t  tx3_disable:1;
        uint8_t  tx4_disable:1;
        uint8_t  reserve:4;
    }byte86;
}qsfp_4x_txDisable;

typedef union
{
    uint8_t power;
    struct 
    {
        uint8_t  power_overRide:1;  //用软件覆盖LP模式信号，设置电源模式
        uint8_t  power_set:1;       //电源设置为低功率模式。默认值为0。
        uint8_t  reserve:6;
    }byte93;
}qsfp_4x_power;

//reg 86-99 rw Control Bytes
struct qsfp_4x_control
{
    

    qsfp_4x_txDisable m_byte86;
    uint8_t m_byte87_92[6];
    qsfp_4x_power m_byte93;
    uint8_t m_byte94_99[6];
};


typedef union
{
    uint8_t mrxlos;
    struct 
    {
        uint8_t  mRx1Los:1;//掩码位，RX LOS通道指示器1
        uint8_t  mRx2Los:1;//掩码位，RX LOS通道指示器2
        uint8_t  mRx3Los:1;//掩码位，RX LOS通道指示器3
        uint8_t  mRx4Los:1;//掩码位，RX LOS通道指示器4
        uint8_t  reserve:4;//预留
    }byte100;
}qsfp_4x_mlos;

typedef union
{
    uint8_t mtxfault;
    struct 
    {
        uint8_t  mTx1Fault:1;//掩码位，TX 故障通道指示器1
        uint8_t  mTx2Fault:1;//掩码位，TX 故障通道指示器2
        uint8_t  mTx3Fault:1;//掩码位，TX 故障通道指示器3
        uint8_t  mTx4Fault:1;//掩码位，TX 故障通道指示器4
        uint8_t  reserve:4;//预留
    }byte101;
}qsfp_4x_mfault;

typedef union
{
    uint8_t mtemp;
    struct 
    {
        uint8_t  reserve:4;//预留
        uint8_t  mlTempWarn:1;//低温警告掩码位
        uint8_t  mhTempWarn:1;//高温警告掩码位
        uint8_t  mlTempAlarm:1;//低温警报掩码位
        uint8_t  mhTempAlarm:1;//高温报警掩码位
    }byte103;
}qsfp_4x_mtemp;

typedef union
{
    uint8_t mvcc;
    struct 
    {
        uint8_t  reserve:4;
        uint8_t  mlVccWarn:1;//低电压警告掩码位
        uint8_t  mhVccWarn:1;//高电压警告掩码位
        uint8_t  mlVccAlarm:1;//低电压报警掩码位
        uint8_t  mhVccAlarm:1;//高电压报警掩码位
    }byte104;
}qsfp_4x_mvcc;

//reg 100~106 RW Module and Channel Masks
struct qsfp_4x_channelMask
{
    
    qsfp_4x_mlos m_byte100;
    qsfp_4x_mfault m_byte101;
    uint8_t m_byte102;
    qsfp_4x_mtemp m_byte103;
    qsfp_4x_mvcc m_byte104;
    uint8_t m_byte105;
    uint8_t m_byte106;

};



//北亿纤通科技有限公司
/**********************Lower Memory Map of the QSFP+ module.***************************/
/*Byte Address  Description                 Type
*0              Identifier(1 Byte)          Read Only
*1-2            Status(2 Bytes)             Read Only
*3-21           Interrupt Flags(19 Bytes)   Read Only
*22-33          Module Monitors(12Bytes)    Read Only
*34-81          Channel Monitors(48 Bytes)  Read Only
*82-85          Reserved (4 Bytes)          Read Only
*86-97          Control(12Bytes)            Read/Write
*98-99          Reserved(2 Bytes)           Read/Write
*100-106        Module and Channel Masks (7 Bytes) Read/Write
*/
/***************************************************************************************/
struct __attribute__ ((__packed__)) qsfp_4x_cache
{
    uint8_t m_identifier;                   //标识符 0xd
    uint8_t m_byte1;                        //预留
    qsfp_4x_status m_byte2;                 //状态
    qsfp_4x_intc_flag0 m_byte3;             //中断标志寄存器
    qsfp_4x_fault m_byte4;                  //故障寄存器
    uint8_t m_byte5;                        //预留
    qsfp_4x_monitorTamp m_byte6;            //温度寄存器
    qsfp_4x_monitorVolt m_byte7;            //电压寄存器
    uint8_t m_byte8;                        //预留
    qsfp_4x_monitorRx1 m_byte9;             //接收通道寄存器
    qsfp_4x_monitorRx2 m_byte10;            //接收通道寄存器
    txBias1_4x m_byte11;                    //发射偏置电流寄存器
    txBias2_4x m_byte12;                    //发射偏置电流寄存器
    qsfp_4x_monitorTx1 m_byte13;            //发射通道寄存器
    qsfp_4x_monitorTx2 m_byte14;            //发射通道寄存器
    uint8_t m_byte15_21[7];                 //预留
    uint8_t m_inTemp[2];                    //温度(MSB LSB)高位在前(–128℃ to +127℃)(2^6~2^-8)16-bit signed
    uint8_t m_byte24_25[2];                 //预留
    uint8_t m_inVoltage[2];                 //电压(MSB LSB)高位在前(uint16_t * 100 μVolt)(0~6.55V)16-bit unsigned integer
    uint8_t m_byte28_33[6];                 //预留
    uint8_t m_inRx1Power[2];                //RX1通道功率(MSB LSB)高位在前,RX input power
    uint8_t m_inRx2Power[2];                //RX2通道功率(MSB LSB)高位在前
    uint8_t m_inRx3Power[2];                //RX3通道功率(MSB LSB)高位在前
    uint8_t m_inRx4Power[2];                //RX4通道功率(MSB LSB)高位在前
    uint8_t m_inTx1Bias[2];                 //TX1通道偏置电流(MSB LSB)高位在前 内部偏置电流
    uint8_t m_inTx2Bias[2];                 //TX2通道偏置电流(MSB LSB)高位在前 内部偏置电流
    uint8_t m_inTx3Bias[2];                 //TX3通道偏置电流(MSB LSB)高位在前 内部偏置电流
    uint8_t m_inTx4Bias[2];                 //TX4通道偏置电流(MSB LSB)高位在前 内部偏置电流

    uint8_t m_inTx1Power[2];                //TX1通道功率(MSB LSB)高位在前,TX output power
    uint8_t m_inTx2Power[2];                //TX2通道功率(MSB LSB)高位在前,TX output power
    uint8_t m_inTx3Power[2];                //TX3通道功率(MSB LSB)高位在前,TX output power
    uint8_t m_inTx4Power[2];                //TX4通道功率(MSB LSB)高位在前,TX output power

    uint8_t m_txMonitorVcc[2];              //TX通道电压(MSB LSB)高位在前
    uint8_t m_rxVcc[2];                     //RX通道电压(MSB LSB)高位在前Internally measured RX VCC
    uint8_t m_monitorVcc1[2];               //电压监控(MSB LSB)高位在前
    uint8_t m_monitorTemp1[2];              //温度监控(MSB LSB)高位在前
    uint8_t m_byte66_85[20];                //预留
    //86-99
    qsfp_4x_txDisable m_byte86;             //控制字节 发射通道失能
    uint8_t m_byte87_92[6];                 //预留
    qsfp_4x_power m_byte93;                 //电源控制字节
    uint8_t m_byte94_99[6];                 //预留
    //100-106 mask
    qsfp_4x_mlos m_byte100;                 //LOS通道掩码模块
    qsfp_4x_mfault m_byte101;               //故障通道掩码模块
    uint8_t m_byte102;                      //预留
    qsfp_4x_mtemp m_byte103;                //温度掩码
    qsfp_4x_mvcc m_byte104;                 //电压模块掩码
    uint8_t m_byte105;                      //预留
    uint8_t m_byte106;                      //预留
    //107-127 Password and Page Select(Read/Write)
    uint8_t m_byte107_118[12];      //reserved
    uint8_t m_byte119_122[4];       //Password Change Entry Area (optional)密码更改输入区
    uint8_t m_byte123_126[4];       //Password Entry Area(optional)密码输入区
    uint8_t m_byte127;              //Page Select Byte default 00h (地址选择字节)

};

/*
*00h default
*01h Not implemented for user. 
*02h Page 02 is optionally provided as user writable EEPROM. The host system may read
*    or write this memory for any purpose
*03h ByteAddress            Description Type
*    128-175             Module Thresholds(48 Bytes) Read-Only
*    176-223             Channel Thresholds(48 Bytes) Read-Only
*    224-225             Reserved(2 Bytes) Read-Only
*    226-239             Vendor Specific Channel Controls(14 Bytes) Read/Write
*    240-241             Optional Channel Controls(2 Bytes) Read/Write
*    242-253             Channel Monitor Masks(12 Bytes) Read/Write
*    254-255             Reserved(2 Bytes) Read/Write
*/

//reg 128-255
struct __attribute__ ((__packed__)) upperMemory00h
{
    uint8_t m_identifier;           //标识符 0Dh=QSFP+
    uint8_t m_extIdentifier;        //拓展标识符
    uint8_t m_connector;            //连接器类型代码
    uint8_t m_transceiver[8];       //有关电子兼容性或光学兼容性的代码
    uint8_t m_encoding;             //串行编码算法的代码
    uint8_t m_bitRate;              //标称比特率，以100 MBits/s为单位
    uint8_t m_rateSelect;           //针对扩展速率选择符合性的标签
    uint8_t m_smfLength;            //SMF光纤支持的链路长度，单位为公里/km
    uint8_t m_ebwLength;            //为EBW 50/125 um光纤支持的链路长度，单位为2米
    uint8_t m_fiber50;              //链路长度支持50/125 um光纤，单位为1米
    uint8_t m_fiber625;             //链路长度支持62.5/125 um光纤，单位为1m
    uint8_t m_copperLen;            //铜支撑的连接长度，单位为1米
    uint8_t m_devTech;              //Device Technology
    uint8_t m_vendorName[16];       //QSFP供应商名称 (ASCII)
    uint8_t m_extTransceiver;       //针对无限波段的扩展收发器代码
    uint8_t m_vendorOUI[3];         //QSFP供应商IEEE公司ID
    uint8_t m_vendorPN[16];         //由QSFP供应商提供的零件号(ASCII)
    uint8_t m_vendorRev[2];         //由供应商提供的零件号的版本级别(ASCII) 
    uint8_t m_waveLength[2];        //标称激光波长（波长=值/20英寸nm)
    uint8_t m_waveLenTolerance[2];  //波长公差(波长=值/20英寸nm)
    uint8_t m_maxCaseTemp;          //最高外壳温度，单位为摄氏度
    uint8_t m_ccBase;               //检查基本ID字段的代码（地址128-190)
    //Extended ID Fields 192-223
    uint8_t m_options[4];           //[0]速率选择，[1]TX禁用，[2]TX故障，[3]LOS
    uint8_t m_vendorSN[16];         //由供应商提供的序列号 (ASCII)
    uint8_t m_dateCode[8];          //供应商的制造日期代码
    uint8_t m_DiagMonitorType;      //指示在收发器中执行的类型的诊断监视（如果有）。保留位1、0
    uint8_t m_enhancedOpt;          //指示在收发器中实现了哪些可选的增强功能。
    uint8_t m_byte222;              //Reserved
    uint8_t m_ccExt;                //检查扩展ID字段的代码（地址192-222)
    //Vendor Specific ID Fields 224-255
    uint8_t m_vendorSpecific[32];   //供应商特定EEPROM
};
